package com.lu.netty.data

/**
 * FileName : NettyVo
 * Author : luqihua
 * Date : 2024/11/28
 * Desc : 消息封装
 **/

data class NettyMessageVo(
    //数据类型
    val dataType: DataType,
    // 业务消息类型, 客户端自定义
    val messageType: Int = 0,
    // 保留字段
    val extra: String = "",
    //业务消息json串,
    val message: String = "{}"
) {
    companion object {
        private val jsonAdapter = tMoshi.adapter(NettyMessageVo::class.java)

        @Throws(Exception::class)
        fun fromJson(json: String): NettyMessageVo? {
            return jsonAdapter.fromJson(json)
        }

        fun createHeartbeat(heartbeat: String): NettyMessageVo {
            return NettyMessageVo(dataType = DataType.HEARTBEAT, message = heartbeat)
        }

        fun createContent(content: String): NettyMessageVo {
            return NettyMessageVo(dataType = DataType.CONTENT, message = content)
        }
    }

    fun toJson(): String {
        return jsonAdapter.toJson(this)
    }
}
